﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace Mangox.Common
{
    public class OtherHelper
    {
        /// <summary>
        /// 传入txt文件路径参数 读取txt文件所有内容 返回DATATABLE
        /// 如用于文本格式Excel
        /// 不完善
        /// </summary>
        /// <param name="pths"></param>
        /// <returns></returns>
        //public static DataTable GetDataTableByTextFile(string pths)
        //{
        //    StreamReader sr = new StreamReader(pths, Encoding.GetEncoding("GB2312"));
        //    string txt = sr.ReadToEnd().Replace("\r\n", "-");
        //    string[] nodes = txt.Split('-');
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("ID", typeof(string));
        //    foreach (string node in nodes)
        //    {
        //        string[] strs = node.Split('-');
        //        DataRow dr = dt.NewRow();
        //        dr["ID"] = strs[0];
        //        dt.Rows.Add(dr);
        //    }
        //    return dt;
        //}


        /// <summary>
        /// 判断字符串是否是数字
        /// </summary>
        public static bool IsNumber(string s)
        {
            if (string.IsNullOrWhiteSpace(s)) return false;
            const string pattern = "^[0-9]*$";
            Regex rx = new Regex(pattern);
            return rx.IsMatch(s);
        }

        /// <summary>
        /// 判断字符串是否是英文
        /// </summary>
        public static bool IsEnglish(string str)
        {
            Regex regChina = new Regex("^[^\x00-\xFF]");
            Regex regEnglish = new Regex("^[a-zA-Z]");
            if (regEnglish.IsMatch(str))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取枚举描述，枚举说明
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetEnumDescription(Enum enumValue)
        {
            string value = enumValue.ToString();
            FieldInfo field = enumValue.GetType().GetField(value);
            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
            if (objs == null || objs.Length == 0)    //当描述属性没有时，直接返回名称
                return value;
            DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;
        }

        /// <summary>
        /// 对象复制，深拷贝，克隆对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T Clone<T>(T obj)
        {
            T ret = default(T);
            if (obj != null)
            {
                XmlSerializer cloner = new XmlSerializer(typeof(T));
                MemoryStream stream = new MemoryStream();
                cloner.Serialize(stream, obj);
                stream.Seek(0, SeekOrigin.Begin);
                ret = (T)cloner.Deserialize(stream);
            }
            return ret;
        }

        /// <summary>
        /// 删除DataTable空行 整行为空时
        /// 如用于Excel导入
        /// 未测试
        /// </summary>
        /// <param name="dt"></param>
        public void RemoveEmpty(DataTable dt)
        {

            //某列为空删除
            //DataTable dt_im = NPOIHelper.ImportExceltoDt(txtExcelPath.Text.Trim());
            //DataView dv = dt_im.DefaultView;
            //dv.RowFilter = "代码<>'' or 代码 is not null";//代码  为列名
            //dt_im = dv.ToTable();
            
            List<DataRow> removelist = new List<DataRow>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                bool IsNull = true;
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim()))
                    {
                        IsNull = false;
                    }
                }
                if (IsNull)
                {
                    removelist.Add(dt.Rows[i]);
                }
            }
            for (int i = 0; i < removelist.Count; i++)
            {
                dt.Rows.Remove(removelist[i]);
            }
        }

        #region 判断字符串是否是汉字

        /// <summary>
        /// 用 UNICODE 编码范围判断字符是不是汉字
        /// </summary>
        /// <param name="text">待判断字符或字符串</param>
        /// <returns>真：是汉字；假：不是</returns>
        public bool CheckStringChineseUn(string text)
        {
            bool res = false;
            foreach (char t in text)
            {
                if (t >= 0x4e00 && t <= 0x9fbb)
                {
                    res = true;
                    break;
                }
            }
            return res;
        }

        /// <summary>
        /// 用 ASCII 码范围判断字符是不是汉字
        /// </summary>
        /// <param name="text">待判断字符或字符串</param>
        /// <returns>真：是汉字；假：不是</returns>
        public bool CheckStringChinese(string text)
        {
            bool res = false;
            foreach (char t in text)
            {
                if ((int)t > 127)
                    res = true;
            }
            return res;
        }

        /// <summary>
        /// 用 正则表达式 判断字符是不是汉字
        /// </summary>
        /// <param name="text">待判断字符或字符串</param>
        /// <returns>真：是汉字；假：不是</returns>
        public bool CheckStringChineseReg(string text)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(text, @"[\u4e00-\u9fbb]");
        }

        #endregion


        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="str"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string SubString(string str, int num)
        {
            if (!string.IsNullOrEmpty(str) && str.Length > num)
            {
                return str.Substring(0, num);
            }
            else
            {
                return str;
            }
        }

    }
}
